const loginColumn = [
  {
    field: 'no',
    title: '序号',
    align: 'center',
    formatter: function(value, row, index) {
      return index + 1;
    }
  },
  {
    field: 'createDate',
    title: '操作时间',
  },
  {
    field: 'username',
    title: '用户名'
  },
  {
    field: 'name',
    title: '姓名',
  },
  {
    field: 'operation',
    title: '用户操作',
  },
  {
    field: 'time',
    title: '在线时长',
  },
  {
    field: 'ip',
    title: 'IP地址',
  }
];

const operateColumn = [
  {
    field: 'no',
    title: '序号',
    align: 'center',
    formatter: function(value, row, index) {
      return index + 1;
    }
  },
  {
    field: 'createDate',
    title: '操作时间',
  },
  {
    field: 'username',
    title: '用户名'
  },
  {
    field: 'name',
    title: '姓名',
  },
  {
    field: 'operation',
    title: '用户操作',
  },
  {
    field: 'method',
    title: '请求方法',
  },
  {
    field: 'params',
    title: '请求参数',
  },
  {
    field: 'time',
    title: '执行耗时',
  },
  {
    field: 'operationStatus',
    title: '操作状态',
  }
];

dayLog = {
  initPage:function() {
    this.data = {
      queryData: {
        startTime: "",
        endTime: "",
        username: "",
        type: "2", // 1 操作日志  2 登陆日志
        page: 1,
        size: 10
      },
      pageNum: 1,
      dataResult: [],
      total: 0,
      loginColumn: loginColumn,
      operateColumn: operateColumn
    };
    this.initDateTime();
    this.getLoginLog();
    this.btnEvent();
  },
  initBootTable(data,columns,total) {
    $('#cnf_reportTable').bootstrapTable({
      method: 'get',
      cache: false,
      height: 420,
      pagination: true,
      pageSize: dayLog.data.queryData.size,
      pageNumber:dayLog.data.queryData.page,
      pageList: [10, 25, 50, 100],
      showColumns: true,
      striped: true,
      columns: columns,
      data :data,
      sidePagination: 'server', // client or server
      totalRows: total,
      onPageChange: function (size, number) {
        dayLog.data.queryData.size = size;
        dayLog.data.queryData.page = number;
        dayLog.queryTableData();
      },
      formatNoMatches: function(){
          return '暂无数据';
      }
    })
  },
  initDateTime() {
    //时分年月日范围
    $('.J-datepickerTime-range').datePicker({
      format: FMT_DTM,
      isRange: true
    });
  },
  getLoginLog() { // 登陆日志
    this.data.queryData.startTime = $(".daylog_start").val();
    this.data.queryData.endTime = $(".daylog_end").val();
    this.data.queryData.username = $(".daylog_username input").val();
    this.data.queryData.type  = "2";
    jrequest.get("/api/sysLog",this.data.queryData).then(res => {
      this.data.dataResult = res.data;
      this.data.total = res.total;
      this.createTableLogin();
    })
  },
  createTableLogin() {
    $('#cnf_reportTable').bootstrapTable('destroy');
    this.initBootTable(this.data.dataResult,this.data.loginColumn,this.data.total);
  },
  createTableOperate() {
    $('#cnf_reportTable').bootstrapTable('destroy');
    this.initBootTable(this.data.dataResult,this.data.operateColumn,this.data.total);
  },
  getOperateLog() { // 操作日志
    $("#jid_loading").show()
    this.data.queryData.startTime = $(".daylog_start").val();
    this.data.queryData.endTime = $(".daylog_end").val();
    this.data.queryData.username = $(".daylog_username input").val();
    this.data.queryData.type = "1";
    jrequest.get("/api/sysLog", this.data.queryData).then(res => {
      this.data.dataResult = res.data;
      this.data.total = res.total;
      this.createTableOperate();
      $("#jid_loading").hide()
    })
  },
  queryTableData() {
    switch(this.data.pageNum) {
      case 1:
        this.getLoginLog();
        break;
      case 2:
        this.getOperateLog();
        break;
    }
  },
  btnEvent() {
    $(".daylogSearch_btn").click(() => { // 查询
      this.queryTableData();
    })
    $(".daylogReset_btn").click(() => { // 重置
      this.data.queryData.startTime = $(".j-search-row .item:first-child input").eq(0).val("");
      this.data.queryData.endTime = $(".j-search-row .item:first-child input").eq(1).val("");
      this.data.queryData.username = $(".daylog_username input").val("");
    })

    $(".tab_child").on("click","div",function() {
      var index = $(this).index();
      $(".tab_child div").eq(index).addClass("childActive").siblings().removeClass("childActive");
      switch(index) {
        case 0:
          dayLog.data.pageNum = 1;
          dayLog.getLoginLog();
          break;
        case 1:
          dayLog.data.pageNum = 2;
          dayLog.getOperateLog();
          break;
      }
    })
  }
}

$(function() {
  dayLog.initPage();
})
